//
//  KLSdk.m
//  WebRTCDemo_iOS
//
//  Created by szblsx2 on 2020/3/5.
//  Copyright © 2020 szblsx2. All rights reserved.
//

#import "KLSdk.h"
#import "KLEngine.h"

@interface KLSdk ()
//连接器
@property (nonatomic, strong) KLEngine *mKLEngine;
//日志管理
@property (nonatomic, strong) RTCFileLogger *flieLogger;

@end

@implementation KLSdk

static KLSdk *shareInstance;

+ (instancetype)shared{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareInstance = [[self alloc] init];
    });
    return shareInstance;
}

- (instancetype)init{
    if (self = [super init]) {
        if (!_mKLEngine) {
            _mKLEngine = [[KLEngine alloc] init];
        }
    }
    return self;
}

#pragma mark - Public Method

/** 初始化SDK */
- (void)initSDKWithServer:(NSString *)server port:(NSString *)port name:(NSString *)name
{
    [self.mKLEngine initEngineWithServer:server port:port name:name];
}

/**设置turn转发服务器地址*/
- (void)setupIceServers:(NSArray<RTCIceServer *> *)iceServers
{
    [self.mKLEngine setupIceServers:iceServers];
}

/**设置视频渲染窗口*/
- (void)setupVideoRenderer:(UICollectionView *)videoRenderer
{
    [self.mKLEngine setupVideoRenderer:videoRenderer];
}

/**创建会议*/
- (void)createMeetingWithRoomId:(NSString *)roomId
{
    [self startWebRTCFileLogger];
    [self.mKLEngine createMeetingWithRoomId:roomId];
}

/**加入会议*/
- (void)joinMeetingWithRoomId:(NSString *)roomId
{
    [self startWebRTCFileLogger];
    [self.mKLEngine joinMeetingWithRoomId:roomId];
}

/**离开会议*/
- (void)leaveMeeting
{
    [self.mKLEngine leaveMeeting];
    [self.mKLEngine freeSdk];
    [self stopWebRTCFileLogger];
}

/**是否打开摄像头*/
- (void)turnOnCamera:(BOOL)open
{
    [self.mKLEngine turnOnCamera:open];
}

/**切换前后置摄像头*/
- (void)switchCamera
{
    [self.mKLEngine switchCamera];
}

/**是否打开麦克风*/
- (void)turnOnMicrophone:(BOOL)open
{
    [self.mKLEngine turnOnMicrophone:open];
}

/**是否扬声器*/
- (void)whetherToOpenLoudspeaker:(BOOL)open
{
    [self.mKLEngine whetherToOpenLoudspeaker:open];
}

/**SDK事件处理*/
- (void)onEventHandleCallBack:(void(^)(NSDictionary *response))callBack;
{
    self.mKLEngine.eventHandleBlock = callBack;
}

- (void)test1
{
    [self.mKLEngine test1];
}

#pragma mark - Private Method

/**开启日志输出*/
- (void)startWebRTCFileLogger
{
    if (!_flieLogger) {
        BOOL createPath = YES;
        NSString *cachesDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
        NSString *dataFilePath = [cachesDirectory stringByAppendingPathComponent:@"LKRTCLogger"];
        if (![[NSFileManager defaultManager] fileExistsAtPath:dataFilePath isDirectory:&createPath]) {
            [[NSFileManager defaultManager] createDirectoryAtPath:dataFilePath withIntermediateDirectories:YES attributes:nil error:nil];
        }
        _flieLogger = [[RTCFileLogger alloc] initWithDirPath:dataFilePath maxFileSize:1024*1024*1024];
        _flieLogger.severity = RTCLoggingSeverityError;
    }
    [_flieLogger start];
}

/**停止日志输出*/
- (void)stopWebRTCFileLogger
{
    if (_flieLogger) {
        [_flieLogger stop];
    }
}

@end
